package 反射.cglib动态代理.v1;

import java.lang.reflect.Method;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class CglibProxy implements MethodInterceptor {
	private static Logger logger = LoggerFactory.getLogger(Programmer.class);

	@Override
	public Object intercept(Object proxy, Method method, Object[] params, MethodProxy methodProxy) throws Throwable {
		logger.info("before invoke:" + method.getName() + " - " + System.currentTimeMillis());
		Object retObj = methodProxy.invokeSuper(proxy, params);
		logger.info("after invoke:" + method.getName() + " - " + System.currentTimeMillis());
		return retObj;
	}

	// 返回目标对象的代理对象
	public Object newProxy(Object target) {
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(target.getClass());
		enhancer.setCallback(this);
		enhancer.setClassLoader(target.getClass().getClassLoader());
		return enhancer.create();
	}

}